package com.magicborrow.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.TagBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class SearchResultFilterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOT = 0;
    private static final int TYPE_NORMAL = 1;
    private ArrayList<StuffBean> data;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private DisplayImageOptions options;

    public SearchResultFilterAdapter(Context pContext){
        this.mContext = pContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setData(ArrayList<StuffBean> pData){
        this.data = pData;
    }

    public ArrayList<StuffBean> getData(){
        return this.data;
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOT;
        }else{
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_FOOT){
            View view = mLayoutInflater.inflate(R.layout.load_more_footer,null);
            view.setTag("foot");
            return new FootViewHolder(view);
        }else if(viewType == TYPE_NORMAL){
            View view = mLayoutInflater.inflate(R.layout.search_result_filter_item,null);
            return new SearchResultFilterViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof SearchResultFilterViewHolder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, BorrowStuffDetailActivity.class);
                    intent.putExtra("data", data.get(position));
                    mContext.startActivity(intent);

                }
            });

            ((SearchResultFilterViewHolder) holder).tvName.setText(data.get(position).getUser().getNickname());
            ((SearchResultFilterViewHolder) holder).rbPingfen.setRating(data.get(position).getUser().getLendScaleAVG());
            ((SearchResultFilterViewHolder) holder).tvPingFen.setText("" + StringUtil.double2String(data.get(position).getUser().getLendScaleAVG()) + "（" + data.get(position).getCommentCount() + "人评价）");
            if (options == null)
                options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(mContext.getResources().getDimensionPixelOffset(R.dimen._30dp))).build();

            ImageLoader.getInstance().displayImage(Constants.ADDRESS + data.get(position).getUser().getAvatar80(),
                    ((SearchResultFilterViewHolder) holder).imvHead, options);
            ((SearchResultFilterViewHolder) holder).llImg.removeAllViews();
            for (StuffBean.ImagesEntity img : data.get(position).getImages()) {
                ImageView imv = createImageView(img.getPreview());
                ((SearchResultFilterViewHolder) holder).llImg.addView(imv);
            }
            ((SearchResultFilterViewHolder) holder).tvAddress.setText(data.get(position).getAddress());
            ((SearchResultFilterViewHolder) holder).stuffName.setText(data.get(position).getName());
            ((SearchResultFilterViewHolder) holder).tvPrice.setText("￥" + data.get(position).getPrice() + "/" + PriceModeEnum.getModeStr(data.get(position).getPriceMode()));
            ((SearchResultFilterViewHolder) holder).llTag.removeAllViews();
            ((SearchResultFilterViewHolder) holder).tvInfo.setText(data.get(position).getDescription());
            for (TagBean s : data.get(position).getTags())
                ((SearchResultFilterViewHolder) holder).llTag.addView(createTag(s.getName()));


        }


    }

    private TextView createTag(String tag) {
        TextView tv = new TextView(mContext);
        tv.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.tag_bg));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.getResources().getDimensionPixelSize(R.dimen.text_size_small_s));
        tv.setTextColor(mContext.getResources().getColor(R.color.orange_red));
        tv.setText("" + tag);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, mContext.getResources().getDimensionPixelOffset(R.dimen._5dp), 0);
        tv.setLayoutParams(params);
        return tv;
    }

    @NonNull
    private ImageView createImageView(String s) {
        final ImageView imv = new ImageView(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(mContext.getResources().getDimensionPixelOffset(R.dimen.layout_padding_nor), 0, 0, 0);
        imv.setScaleType(ImageView.ScaleType.FIT_XY);
        imv.setLayoutParams(params);
        // imv.setImageDrawable(context.getResources().getDrawable(R.mipmap.img_default));
        ImageLoader.getInstance().loadImage(Constants.ADDRESS + s, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // super.onLoadingComplete(imageUri, view, loadedImage);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imv.getLayoutParams();
                //  layoutParams.width = loadedImage.getWidth() * layoutParams.height / loadedImage.getHeight();
                layoutParams.width = loadedImage.getWidth() * mContext.getResources().getDimensionPixelOffset(R.dimen._150dp) / loadedImage.getHeight();
                Log.i("======", layoutParams.width + "===" + layoutParams.height);
                imv.setLayoutParams(layoutParams);
                imv.setImageBitmap(loadedImage);
            }
        });
        //  ImageLoader.getInstance().displayImage(Constants.ADDRESS + s, imv);
        return imv;
    }

    @Override
    public int getItemCount() {
        return data.size()+1;
    }



    public class SearchResultFilterViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAddress;
        private TextView tvPrice;
        private TextView tvPingFen;
        private ImageView imvHead;
        private TextView tvName;
        private RatingBar rbPingfen;
        private LinearLayout llImg;
        private TextView stuffName;
        private TextView tvInfo;
        private LinearLayout llTag;

        public SearchResultFilterViewHolder(View view) {
            super(view);
            imvHead = (ImageView) view.findViewById(R.id.imv_head);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvAddress = (TextView) view.findViewById(R.id.tv_address);
            tvPingFen = (TextView) view.findViewById(R.id.tv_pingfen);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            tvInfo = (TextView) view.findViewById(R.id.tv_info);
            rbPingfen = (RatingBar) view.findViewById(R.id.rb_pingfen);
            llImg = (LinearLayout) view.findViewById(R.id.ll_img);
            stuffName = (TextView) view.findViewById(R.id.stuff_name);
            llTag = (LinearLayout) view.findViewById(R.id.ll_tag);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
